package mark.tom.pistols.model;

import java.util.ArrayList;
import java.util.List;

public final class SecurityModelProto {

    public static final class SecurityModel {

        private List<EncryptFile> encryptFiles;
        private String decryptKey;
        private String method;
        private String extParam;

        SecurityModel(List<EncryptFile> encryptFiles, String decryptKey, String method, String extParam) {
            this.encryptFiles = encryptFiles;
            this.decryptKey = decryptKey;
            this.method = method;
            this.extParam = extParam;
        }

        public List<EncryptFile> getEncryptFiles() {
            return encryptFiles;
        }

        public String getDecryptKey() {
            return decryptKey;
        }

        public String getExtParam() {
            return extParam;
        }

        public String getMethod() {
            return method;
        }

        public static Builder newBuilder() {
            return new Builder();
        }

        public static final class Builder {

            private final List<EncryptFile> encryptFiles = new ArrayList<EncryptFile>();
            private String decryptKey;
            private String method;
            private String extParam;

            public Builder addInclude(EncryptFile value) {
                this.encryptFiles.add(value);
                return this;
            }

            public Builder setDecryptKey(String value) {
                this.decryptKey = value;
                return this;
            }

            public Builder setMethod(String method) {
                this.method = method;
                return this;
            }

            public Builder setExtParam(String extParam) {
                this.extParam = extParam;
                return this;
            }

            public SecurityModel build() {
                return new SecurityModel(encryptFiles, decryptKey, method, extParam);
            }
        }

        public static final class EncryptFile {

            private final String name;

            EncryptFile(String name) {
                this.name = name;
            }

            public String getName() {
                return name;
            }

            public static Builder newBuilder() {
                return new Builder();
            }

            public static final class Builder {

                private String name;

                public Builder setName(String value) {
                    this.name = value;
                    return this;
                }

                public EncryptFile build() {
                    return new EncryptFile(name);
                }
            }
        }

        public byte[] toByteArray() {
            StringBuilder sb = new StringBuilder("");
            sb.append("<");
            if (null != decryptKey) {
                sb.append(decryptKey);
            }
            sb.append(">");
            sb.append("<");
            if (null != method) {
                sb.append(method);
            }
            sb.append(">");
            sb.append("<");
            if (null != extParam) {
                sb.append(extParam);
            }
            sb.append(">");
            sb.append("&");
            sb.append("<");
            StringBuilder fileSb = new StringBuilder();
            if (null != encryptFiles && encryptFiles.size() > 0) {
                for (int i = 0; i < encryptFiles.size(); i++) {
                    if (null != encryptFiles.get(i).getName()) {
                        fileSb.append(encryptFiles.get(i).getName());
                        if (i != (encryptFiles.size() - 1)) {
                            fileSb.append("|");
                        }
                    }
                }
            }
            sb.append(fileSb);
            sb.append(">");
            byte[] result;
            try {
                result = sb.toString().getBytes("UTF-8");
            } catch (Exception ex) {
                result = sb.toString().getBytes();
            }
            return result;
        }

        public static SecurityModel parseFrom(byte[] data) {
            SecurityModel result = null;
            try {
                String[] splitData = new String(data, "UTF-8").split("&");
                if (splitData.length != 4) {
                    throw new IllegalArgumentException();
                }
                String split1 = splitData[0];
                String decryptKey = split1.substring(1, split1.length() - 1);
                String split2 = splitData[1];
                String method = split2.substring(1, split2.length() - 1);
                String split3 = splitData[2];
                String extParam = split3.substring(1, split3.length() - 1);
                String split4 = splitData[3];
                String listFileStr = split4.substring(1, split4.length() - 1);
                String[] fileArray = listFileStr.split("\\|");
                List<EncryptFile> fileList = new ArrayList<EncryptFile>();
                for (String fileName : fileArray) {
                    fileList.add(new EncryptFile(fileName));
                }
                result = new SecurityModel(fileList, decryptKey, method, extParam);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return result;
        }
    }
}
